﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CCH.DAO;
using CCH.Entity;

public partial class _20_2001_1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            sysfunctionDAO dao = new sysfunctionDAO();
            string title = dao.Get_titleName(dao.Get_pathNO(2001, Request["type"]));
            this.DropDownList1.DataBind();

            if (Request.QueryString["mode"] == "new")
            {
                this.lab_title.Text = title + " - 新增";
                this.Panel1.Visible = false;
                this.calendar1._ADDate = DateTime.Now;
            }
            else
            {
                this.lab_title.Text = title + " - 修改";
                this.Panel2.Visible = false;

                this.hidd_no.Value = Request.QueryString["ID"];
                int mes_no = int.Parse(this.hidd_no.Value);

                message data = new messageDAO().Get_message(mes_no);

                this.DropDownList1.Items.FindByValue(data.cla_no.ToString()).Selected=true;
                this.tbox_title.Text = data.mes_title.ConvertToNewLine();
                this.tbox_content.Text = data.mes_content.ConvertToNewLine();
                this.calendar1._ADDate = data.mes_datetime.Value;

                //附檔
                this.ObjectDataSource1.SelectParameters["mes_no"].DefaultValue = this.hidd_no.Value;
                this.GridView1.DataBind();
            }
            

            
        }
    }

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        this.DropDownList1.Focus();
    }

    protected void btn_ok_Click(object sender, EventArgs e)
    {
        if (check_Input())
        {
            string str = "";
            messageDAO dao = new messageDAO();
            message data;

            if (Request.QueryString["mode"] == "new")
            {
                data = new message();
                data.state = "1";
                data.sfu_no = new sysfunctionDAO().Get_pathNO(2001, Request["type"]);//此筆資料是屬於何種功能
                str = "新增完成!";
            }
            else
            {
                data = dao.Get_message(int.Parse(this.hidd_no.Value));
                str = "修改完成!";
            }

            data.cla_no = Convert.ToInt32(this.DropDownList1.SelectedValue);
            data.mes_title = this.tbox_title.Text.ConvertToBR();
            data.mes_content = this.tbox_content.Text.ConvertToBR();
            data.mes_datetime = this.calendar1._ADDate;
            data.createuid = new SessionObject().SessionUserID;
            data.createtime = DateTime.Now;

            if (Request.QueryString["mode"] == "new")//新增
            {
                dao.Add_message(data);
                dao.Update();

                int mes_no = data.mes_no;

                //存檔目錄
                string root = System.Configuration.ConfigurationManager.AppSettings["FileUpload"];

                messagefileDAO filedao = new messagefileDAO();

                //附檔
                for (int i = 2; i <= 6; i++)
                {
                    FileUpload file = ((FileUpload)this.FindControl("FileUpload" + i));
                    if (file.HasFile)
                    {
                        //副檔名
                        string exi = file.FileName.Split('.')[1].ToLower();
                        //檔名
                        string filename = Guid.NewGuid().ToString("N") + "." + exi;
                        //上傳
                        file.SaveAs(root + filename);

                        messagefile filedata = new messagefile();
                        filedata.mess_no = mes_no;
                        filedata.file_name = file.FileName;//原始檔名
                        filedata.file_type = exi;
                        filedata.file_path = root + filename;//連同新檔名

                        filedao.Add_messagefile(filedata);
                        filedao.Update();
                    }
                }
            }
            else
            {
                dao.Update();
            }

            //更新父代網頁
            JsUtil.UpdateParentJs(this, str);
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (this.FileUpload1.HasFile)
        {
            //存檔目錄
            string root = System.Configuration.ConfigurationManager.AppSettings["FileUpload"];

            //副檔名
            string exi = this.FileUpload1.FileName.Split('.')[1].ToLower();
            //檔名
            string filename = Guid.NewGuid().ToString("N") + "." + exi;
            //上傳
            this.FileUpload1.SaveAs(root + filename);

            messagefile filedata = new messagefile();
            filedata.mess_no = int.Parse(this.hidd_no.Value);
            filedata.file_name = this.FileUpload1.FileName;//原始檔名
            filedata.file_type = exi;
            filedata.file_path = root + filename;//連同新檔名

            messagefileDAO filedao = new messagefileDAO();
            filedao.Add_messagefile(filedata);
            filedao.Update();

            this.ObjectDataSource1.SelectParameters["mes_no"].DefaultValue = this.hidd_no.Value;
            this.GridView1.DataBind();
        }
    }

    private bool check_Input()
    {
        if (string.IsNullOrEmpty(this.tbox_title.Text))
        {
            JsUtil.AlertJs(this, "請輸入標題!");
            return false;
        }

        if (string.IsNullOrEmpty(this.tbox_content.Text))
        {
            JsUtil.AlertJs(this, "請輸入內容!");
            return false;
        }

        if (!this.calendar1.CheckDateTime())
        {
            JsUtil.AlertJs(this, "請輸入正確日期!");
            return false;
        }

        return true;
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int rowIndex = System.Convert.ToInt32(e.CommandArgument);
        int id = System.Convert.ToInt32(this.GridView1.DataKeys[rowIndex].Value.ToString());

        //停用
        if (e.CommandName.Equals("disable"))
        {
            new messagefileDAO().Delete_messagefile(id);
            this.ObjectDataSource1.SelectParameters["mes_no"].DefaultValue = this.hidd_no.Value;
            this.GridView1.DataBind();
        }
    }
}